Optimization of a function

I have written a function to remove new lines and semi columns. The function iterates all chars in a block of text. In our application, as a whole, the consumes 6 minutes. Is there a way to to optimize the function?

string remNewLine(string str)
{
Buffer buf_in = create;
Buffer buf_out = create;

buf_in = str "";

int i_len;
i_len = length(buf_in);

for (i=0; i < i_len; i+=1)
{
if (buf_in[i] == '\n')
{
buf_out += " "
}
else
{
if (buf_in[i] == ';')
{
buf_out += ","
}
else
{
buf_out += buf_in[i]
}
}
}
return stringOf(buf_out)
}


SystemAdmin - Thu Apr 23 04:54:39 EDT 2009

Re: Optimization of a function
SystemAdmin - Thu Apr 23 07:33:54 EDT 2009

Would this work better:
 

string remNewLine(string str)
{
Buffer buf_in = create
buf_in = str
int i_len = length(buf_in)
int i = 0
 
for (i=0; i < i_len; i++)
{
if ((buf_in[i] == '\n') || (buf_in[i] == ';'))
{
  set(buf_in,i,' ')
}
}
 str = stringOf(buf_in)
 delete buf_in
 buf_in = null
 return str
}
 
It uses the internal set function to replace characters and thus needs only one buffer and also clears that after use, so the buffers are not left in memory.

Re: Optimization of a function
SystemAdmin - Thu Apr 23 08:41:49 EDT 2009

SystemAdmin - Thu Apr 23 07:33:54 EDT 2009

Would this work better:
 

string remNewLine(string str)
{
Buffer buf_in = create
buf_in = str
int i_len = length(buf_in)
int i = 0
 
for (i=0; i < i_len; i++)
{
if ((buf_in[i] == '\n') || (buf_in[i] == ';'))
{
  set(buf_in,i,' ')
}
}
 str = stringOf(buf_in)
 delete buf_in
 buf_in = null
 return str
}
 
It uses the internal set function to replace characters and thus needs only one buffer and also clears that after use, so the buffers are not left in memory.

Thanks!!!

Re: Optimization of a function
llandale - Thu Apr 23 14:57:24 EDT 2009

Yes, using the same buffer works a little better, but only because you are replacing characters with exactly one other character. If you want to replace all the EOLs with two spaces, they you need your solution.

Since you are moving characters around (and not strings), I don't think you have an string-table explosion problem.

I think the main problem with your solution is failure to delete the internal buffers. I now routinely solve that problem by using 'globabal' buffers that only one function uses:
 

Buffer   remNewLine_BufIn  = create()
Buffer   remNewLine_BufOut = create()
string   remNewLine(string str)
{  remNewLine_BufIn  = str
   remNewLine_BufOut = ""
       bla bla bla
   return(stringOf(remNewLins_BufOut)
}  // end remNewLine()

 


Just remember to initially clear the buffers before you use them.

In your case, perhaps instead you could do this:
delete(buf_In)
string Results = stringOf(buf_Out)
delete(buf_Out)
return(Results)

Creating and deleting buffers takes quite a long while, which is why I like my solution for high-frequency functions.

>Louie